package com.ming.ui;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Objects;

public class Register extends JFrame implements MouseListener {
    //与注册有关的内容写在此处
    ArrayList<User> allUsers = new ArrayList<>();
    //注册按钮
    JButton register = new JButton();
    JButton resetting = new JButton();
    //重置按钮
    JButton back = new JButton();
    //返回按钮
    JTextField usernameText = new JTextField();
    //输入用户名
    JTextField password1Text = new JTextField();
    //输入密码
    JTextField password2Text = new JTextField();
    //再次输入密码
    public  Register(ArrayList<User> allUsers){
        //界面的初始化
        initJFrame();
        //有关注册的内容
        initView();
        //传输已经拥有的账户密码
        this.allUsers = allUsers;
        //显现画面
        this.setVisible(true);
    }
    public void initJFrame(){
        //注册界面
        this.setSize(488,390);
        //设置界面的标题
        this.setTitle("拼图 V1.0注册  张德明最帅");
        //设置界面始终置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLayout(null);
        //取消自动居中模式
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public void initView(){

        //1.添加用户名
        JLabel username = new JLabel(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\注册用户名.png"));
        username.setBounds(105,150,79,17);
        this.getContentPane().add(username);

        //2.用户名输入
        usernameText.setBounds(200,135,200,30);
        this.getContentPane().add(usernameText);

        //3.密码框
        JLabel password1 = new JLabel(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\注册密码.png"));
        password1.setBounds(110,200,64,16);
        this.getContentPane().add(password1);

        //4.输入密码
        password1Text.setBounds(200,195,200,30);
        this.getContentPane().add(password1Text);


        //5.第二次密码
        JLabel password2 = new JLabel(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\再次输入密码.png"));
        password2.setBounds(100,250,96,17);
        this.getContentPane().add(password2);


        //6.第二次输入密码
        password2Text.setBounds(200,245,200,30);
        this.getContentPane().add(password2Text);

        //7.注册按钮键
        register.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\注册按钮.png"));
        register.setBounds(150,300,128,47);
        //去除注册的边框
        register.setBorderPainted(false);
        //去除注册的背景
        register.setContentAreaFilled(false);
        //绑定鼠标事件
        register.addMouseListener(this);
        this.getContentPane().add(register);

        //8.重置按钮键
        resetting.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\重置按钮.png"));
        resetting.setBounds(300,300,128,47);
        //去除注册的边框
        resetting.setBorderPainted(false);
        //去除注册的背景
        resetting.setContentAreaFilled(false);
        //绑定鼠标事件
        resetting.addMouseListener(this);
        this.getContentPane().add(resetting);


        //9.设置返回按钮
        back.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\return.jpg"));
        back.setBounds(0,0,100,100);
        //去除返回的边框
        back.setBorderPainted(false);
        //去除背景
        back.setContentAreaFilled(false);
        //绑定鼠标事件
        back.addMouseListener(this);
        //添加按钮
        this.add(back);
        //10.背景画面
        JLabel background = new JLabel(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\background.png"));
        background.setBounds(0,0,470,390);
        this.getContentPane().add(background);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if(source == register){
            User user = new User();
            //获取用户名
            String username1 = usernameText.getText();
            //获取第一次输入的密码
            String password1 = password1Text.getText();
            //获取第二次的密码
            String password2 = password2Text.getText();
            if (username1.isEmpty()){
                showJDialog("用户名未输入");
            } else if (getIndex(username1,allUsers) >= 0) {
                showJDialog("该用户名已经被注册");
            } else if (password1.isEmpty()) {
                showJDialog("密码未输入");
            } else if(!Objects.equals(password2, password1)){
                showJDialog("两次输入的密码不一致");
            }else {
                allUsers.add(new User(username1,password1));
                showJDialog("注册成功，请登录");
                for (User allUser : allUsers) {
                    System.out.println(allUser.getUsername() + ", " + allUser.getPassword());
                }
                this.setVisible(false);
                new Log(allUsers);
            }
        } else if (source == resetting) {
            //使用setText方法
            usernameText.setText("");
            password1Text.setText("");
            password2Text.setText("");
            showJDialog("重置成功");
        } else if (source == back) {
            System.out.println("点击了返回按钮");
            //退出注册界面
            this.setVisible(false);
            //进入登录界面
            new Log(allUsers);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getSource() == register){
            register.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\注册按下.png"));
        } else if (e.getSource() == resetting) {
            resetting.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\重置按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if(e.getSource() == register){
            register.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\注册按钮.png"));
        } else if (e.getSource() == resetting) {
            resetting.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\register\\重置按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
    public int getIndex(String username,ArrayList<User> list){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(username)){
                return i;
            }
        }
        return -1;
    }
    public void showJDialog(String content){
        //创建一个弹窗对象
        JDialog jDialog = new JDialog();
        //设置大小
        jDialog.setSize(200,150);
        //弹框置顶
        jDialog.setAlwaysOnTop(true);
        //居中
        jDialog.setLocationRelativeTo(null);
        //不关闭无法进行其他的操作
        jDialog.setModal(true);

        //创建JLabel对象
        JLabel warning = new JLabel(content);
        warning.setBounds(0,0,200,150);
        jDialog.getContentPane().add(warning);

        //展示
        jDialog.setVisible(true);
    }

}
